package org.flow.basic.doubts.tjbase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xl-9527
 * @since 2024/10/29
 **/
public class ArrayListInThreadDemo {

    private static final Logger log = LoggerFactory.getLogger(ArrayListInThreadDemo.class);


    public void inThread() {
        List<String> all = new ArrayList<>();

        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("{}", all.size());
            }
        }).start();

        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("{}", all.size());
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; ; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                all.add("hello - " + i);
            }
        }).start();

        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
